package com.wtzz.police.base.util.component;

import com.github.dozermapper.core.Mapper;
import com.github.dozermapper.core.MappingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class MapperWrapper {

    private final Mapper mapper;

    @Autowired
    public MapperWrapper(Mapper mapper) {
        this.mapper = mapper;
    }

    public <T> T map(Object source, Class<T> destinationClass) throws MappingException {
        if (source == null) return null;
        else return mapper.map(source, destinationClass);
    }

    public void map(Object source, Object destination) throws MappingException {
        mapper.map(source, destination);
    }

    public <T> T map(Object source, Class<T> destinationClass, String mapId) throws MappingException {
        if (source == null) return null;
        else return mapper.map(source, destinationClass, mapId);
    }

    public void map(Object source, Object destination, String mapId) throws MappingException {
        mapper.map(source, destination, mapId);
    }

    public <S,T> List<T> mapList(List<S> sourceList, Class<T> destinationClass) {
        if (sourceList == null) return null;
        List<T> destinationList = new ArrayList<>();
        for (S source : sourceList) {
            destinationList.add(mapper.map(source, destinationClass));
        }
        return destinationList;
    }

    public <S,D> void mapList(List<S> sourceList, List<D> destinationList, Class<D> destinationClass) {
        for (S source : sourceList) {
            destinationList.add(mapper.map(source, destinationClass));
        }
    }

    public Mapper getRawMapper() {
        return mapper;
    }

}
